<%
    /**
     * 定制显示属性
     *
     * created by amon 2005-12-21
     */
%>
<%@ include file = "/common/ini.jsp"%>
<%@ include file = "/common/_log4j.jsp"%>
<%@ include file = "/common/_ApplicationContext_init.jsp"%> 
<%@ include file = "/common/_currUser_Infor.jsp"%>
<%@ include file = "/common/params/_getParamsInSession.jsp"%>
<%@ include file = "/common/params/iniParamsForm.jsp"%>
<% String configUrl = "/common/List/_configColumn/config.jsp";%>
<%@ include file = "/common/List/_configColumn/getTitleAndList.jsp"%>
<%@ include file = "/js/_js_for_select.jsp"%>
<%@ include file = "/css/detailcss.jsp"%>
<%@ include file = "/common/_vr.jsp"%>
<%
    if(!vr.isAllValidated())    
    {
        %>
        <font color="red">userId<%= vr.getErrMess("userId")%></font>
        <font color="red">configedServiceURL<%= vr.getErrMess("configedServiceURL")%></font>
        <font color="red">columnSort<%= vr.getErrMess("columnSort")%></font>
        <%
        return;
    }    
    String userId = currUser.getId();
%>
<%
    logger.info("开始获取serviceURL" );
    String serviceURL = ((String[])paramsMap.get("serviceURL"))[0];
    ConfigColumnManager configColumnManager = (ConfigColumnManager)applicationContext.getBean("configColumnManager");
    ConfigColumn configColumn;
    if(configColumnManager.getByUserIdAndServiceURL(userId,serviceURL) == null)
    {
        configColumn = null;
    }
    else
    {
        configColumn = (ConfigColumn)configColumnManager.getByUserIdAndServiceURL(userId,serviceURL);
    }
    
%>
<title>定制列</title>
<script>
    function insert()
    {
        try
        {
            addSelected2OtherSelector(ccForm.defaultColumn,ccForm.configColumn);
            selectNone(ccForm.defaultColumn);
        }
        catch(err)
        {
            alert(err);
        }
    }

    function del()
    {
        delSelectedOption(ccForm.configColumn);
    }

    function insertAll()
    {
        selectAll(ccForm.defaultColumn);
        insert();
    }

    function removeAll()
    {
        selectAll(ccForm.configColumn);
        delSelectedOption(ccForm.configColumn);
    }
</script>
<table width="100%" height="100%">
<form name="ccForm" method="post" action="<%= ControllerURL%>">
    <tr>
        <td align="center">
            <b>默认</b>
        </td>
        <td align="center">
            &nbsp;
        </td>
        <td align="center">
            <b>自定义</b>
        </td>
    </tr>
    <tr>
        <td align="center">
            <select name="defaultColumn" size="23" multiple style="width=2.1in;" ondblclick="insert();">
        </td>
        <td width="100" align="center">
            <table>
                <tr>
                    <td><input type="button"  value="选 择 >>" onclick = "insert()" ></td>
                </tr>
                <tr>
                    <td><input type="button"  value="移 除 <<" onclick = "del()" ></td>
                </tr>
                <tr>
                    <td><input type="button"  value="全选择>>" onclick = "insertAll()" ></td>
                </tr>
                <tr>
                    <td><input type="button"  value="全移除<<" onclick = "removeAll()" ></td>
                </tr>
                <tr>
                    <td><input type="button" size="5" value="移到首位" onclick = "move2Head(ccForm.configColumn)" ></td>
                </tr>
                <tr>
                    <td><input type="button" size="5"  value="上    移" onclick = "move2Pre(ccForm.configColumn)" ></td>
                </tr>
                <tr>
                    <td><input type="button"  value="下    移" onclick = "move2Next(ccForm.configColumn)" ></td>
                </tr>
                <tr>
                    <td><input type="button"  value="移到末尾" onclick = "move2End(ccForm.configColumn)" ></td>
                </tr>
            </table>
        </td>
        <td align="center" >
            <select name="configColumn" size="23" multiple style="width=2.1in;" ondblclick="del();">
        </td>
    </tr>
    <input type="hidden" name="serviceURL" >
    <input type="hidden" name="configedServiceURL" >
    <input type="hidden" name="columnSort" >
    <input type="hidden" name="titleSort" >
    <input type="hidden" name="userId" >
    <input type="hidden" name="id" >
</form>
    <tr>
        <td align="center" colspan="3">
            <input type="button" value="提交" onclick="save()">&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" value="重置" onclick="reset()">&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" value="返回" onclick="window.close()">
        </td>
    </tr>
</table>

<script>
    function save()
    {
        ccForm.columnSort.value = "";
        ccForm.titleSort.value = "";
        if(ccForm.configColumn.options.length == 0)
        {
            selectAll(ccForm.defaultColumn);
            addSelected2OtherSelector(ccForm.defaultColumn,ccForm.configColumn);
            selectNone(ccForm.defaultColumn);
        }
        for(var i=0;i < ccForm.configColumn.options.length;i++)
        {
            var strs = ccForm.configColumn.options(i).value.split("_");
            if(strs.length != 2)
            {
                alert("定制列标题和记录列下标号解析失败");
                return;
            }
            ccForm.titleSort.value = ccForm.titleSort.value + "," +strs[0];
            ccForm.columnSort.value = ccForm.columnSort.value + "," +strs[1];
        }
        ccForm.titleSort.value = ccForm.titleSort.value.substring(1,ccForm.titleSort.value.length);
        ccForm.columnSort.value = ccForm.columnSort.value.substring(1,ccForm.columnSort.value.length);
//        alert("title : " + ccForm.titleSort.value);
//        alert("column : " + ccForm.columnSort.value);
        ccForm.submit();
    }

    function reset()
    {
        try
        {
            ccForm.configedServiceURL.value = queryForm.serviceURL.value;
            delAllOptions(ccForm.defaultColumn);
            delAllOptions(ccForm.configColumn); 
            defaultConfig();
            <%
            if(configColumn == null)
            {
                logger.debug("--------------------------------------");
            %>
            ccForm.serviceURL.value = "userdefined.configcolumn.NewConfigColumnService";
            <%
            } //
            else
            {
                logger.debug("+++++++++++++++++++++++++++++++++++++++++");
            %>
            ccForm.serviceURL.value = "userdefined.configcolumn.UpdateConfigColumnService";
            ccForm.id.value = "<%= configColumn.getId()%>";
            // 初始化默认栏目
            try
            {
            <%
                String[] titleSorts =  configColumn.getTitleSorts();
                String[] titleValues = configColumn.getTitleSortsValues();
                for(int i = 0; i < titleValues.length; i ++)
                {
                    String titleValue = titleSorts[i] + "_" + titleValues[i];
                    %>  
//                        alert("<%= titleValue %>" + "  " + getOptionByValue(ccForm.defaultColumn,"<%= titleValue%>"));
                    addOption(ccForm.configColumn,"<%= titleValue%>",getOptionByValue(ccForm.defaultColumn,"<%= titleValue%>").text);
                    <%
                }// for
            %>
            }//js try
            catch(err)
            {
                alert("原来定义的字段已经发生变更,请重新定义");
                delAllOptions(ccForm.configColumn);
            }// js catch
            <%
            }//else
            %>
            ccForm.userId.value = "<%= userId%>";
        }
        catch(err)
        {
            alert(err);
        }
    } // function reset()

    function defaultConfig()
    {
    <%
        // 初始化default框框
        logger.info("构造标题头区");
        ItemTitleArea itArea = (ItemTitleArea)request.getAttribute("viewer");
        List titles = itArea.getTitles(request.getContextPath());
        int index = 0;
        for(int i = 0; i < titles.size(); i ++)
        {
            Title title = (Title)titles.get(i);
            int sumOfLeafs = title.getSumOfLeafs();
            if(sumOfLeafs == 0)
            {
                sumOfLeafs = 1;
            }
            StringBuffer sb = new StringBuffer(sumOfLeafs * 2);
            // 标题的index
            sb.append(i)
              .append("_");
            // 栏目的index
            for(int j = 0; j < sumOfLeafs; j ++)
            {
                sb.append(index)
                  .append("-");
                index ++;
            }// for
            String str = sb.toString();
            str = str.substring(0,str.length() - 1);
            %>
                addOption(ccForm.defaultColumn,"<%= str%>","<%= title.getName()%>");
                
            <%
        } // 标题头for
    %>
    } // function defaultConfig()

    this.reset();
</script>
